package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.ArticlesListDetailsActivity;
import com.zzk.lanouhn.myapplication1.modles.AllRelationArticles;

import java.util.List;

/**
 * 单品详情，全部相关文章适配器
 * Created by lanouhn on 16/9/13.
 */
public class AllRelationArticlesAdapter extends RecyclerView.Adapter<AllRelationArticlesAdapter.articleViewHolder> {

    private Context context;
    private List<AllRelationArticles.DataBean> allRelationArticles;

    public AllRelationArticlesAdapter(Context context, List<AllRelationArticles.DataBean> allRelationArticles) {
        this.context = context;
        this.allRelationArticles = allRelationArticles;
    }

    @Override
    public articleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_articles_singlegood, null);
        articleViewHolder holder = new articleViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(articleViewHolder holder, int position) {
        final AllRelationArticles.DataBean bean = allRelationArticles.get(position);
        holder.tv_title.setText(bean.getTitle());
        holder.tv_name.setText(bean.getUser().getNickname());
        holder.tv_views.setText("浏览 " + bean.getViews());
        holder.tv_comments.setText("评论 " + bean.getComments());
        holder.tv_time.setText(bean.getCreate_time_str());
        Picasso.with(context).load(bean.getUser().getAvatar()).into(holder.iv_icon);
        Picasso.with(context).load(bean.getPics().get(0).getUrl()).into(holder.iv_pic);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ArticlesListDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("内容", bean.getId());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return allRelationArticles.size();
    }

    public class articleViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_title, tv_name, tv_views, tv_comments, tv_time;
        private ImageView iv_icon, iv_pic;

        public articleViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title_article_singlegood);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name_author_singlegood);
            tv_views = (TextView) itemView.findViewById(R.id.tv_view_counts_singlegood);
            tv_comments = (TextView) itemView.findViewById(R.id.tv_article_comments_singlegood);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time_article_singlegood);
            iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon_author_singlegood);
            iv_pic = (ImageView) itemView.findViewById(R.id.iv_pic_article_singlegood);
        }
    }
}
